<template>
  <div>
    <el-button type="primary" @click="addPlate" class="addPlate" size="small">添加板块</el-button>
    <el-table
      border
      tooltip-effect="dark"
      :header-cell-style="{background:'#F9F9F9'}"
      :data="tableData"
      v-loading="tableLoading"
      style="width: 100%;"
    >
      <el-table-column prop="name" align="center" label="板块名称"></el-table-column>
      <el-table-column prop="sort" align="center" label="排序">
        <template slot-scope="scope">
          <el-input
            placeholder="请输入序号"
            v-model="scope.row.sort"
            @keyup.enter.native="saveSort(scope.row)"
          >
            <template slot="append">
              <el-button
                class="el-icon-edit"
                type="danger"
                size="mini"
                @click="saveSort(scope.row)"
              >保存</el-button>
            </template>
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="delPlate(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="添加板块名称"
      :close-on-click-modal="false"
      :show-close="false"
      :visible.sync="dialogStation"
      width="500px"
      center
    >
      <el-form
        status-icon
        label-width="120px"
        :model="form"
        :rules="rules"
        ref="form"
        class="demo-ruleForm"
      >
        <el-form-item label="板块名称" prop="name">
          <el-input v-model.trim="form.name" placeholder="请输入板块名称"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close('form')">取 消</el-button>
        <el-button type="primary" @click="save('form')" :loading="loading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  selectProjectPlate,
  insertProjectPlate,
  setSortProjectPlate,
  deleteProjectPlate,
} from "@/service/Api.js"; //接口
export default {
  components: {},
  name: "projectMent",
  props: ["projectid"],
  data() {
    return {
      tableData: [],
      tableLoading: false,
      dialogStation: false,
      form: {
        name: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入板块名称",
            trigger: ["blur", "change"],
          },
        ],
      },
      loading: false,
    };
  },
  created() {
    this.dataList();
  },
  methods: {
    dataList() {
      selectProjectPlate().then((res) => {
        console.log(res);
        if (res.code == 0) {
          this.tableData = res.data;
        }
      });
    },
    saveSort(row) {
      console.log(row);
      if (!row.sort) {
        this.$message.success("操作成功!");
        return;
      }
      setSortProjectPlate(row.id, row.sort).then((res) => {
        console.log(res);
        if (res.code == 0) {
          this.$message.success("操作成功!");
          this.dataList();
        }
      });
    },
    addPlate() {
      this.dialogStation = true;
    },
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          insertProjectPlate(this.form).then((res) => {
            console.log(res);
            if (res.code == 0) {
              this.$message.success("操作成功!");
              this.close("form");
              this.dataList();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    close(formName) {
      this.$refs[formName].resetFields();
      this.dialogStation = false;
    },
    delPlate(row) {
      this.$confirm("确定要删除改板块", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          deleteProjectPlate(row.id).then((res) => {
            console.log(res);
            if (res.code == 0) {
              this.$message.success("删除成功!");
              this.dataList();
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
.addPlate {
  margin-bottom: 15px;
}
</style>
